/*
 * Copyright (C) 2022 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xjh.him.activity;

import android.view.KeyEvent;

import com.alibaba.fastjson.JSON;
import com.xjh.him.R;
import com.xjh.him.common.Constant;
import com.xjh.him.core.netty.NettyClient;
import com.xjh.him.core.netty.codec.Command;
import com.xjh.him.core.netty.codec.Packet;
import com.xjh.him.dto.user.UserDTO;
import com.xjh.him.utils.MMKVUtils;
import com.xjh.him.utils.SettingUtils;
import com.xjh.him.utils.TokenUtils;
import com.xjh.him.utils.Utils;
import com.xjh.him.utils.XToastUtils;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.KeyboardUtils;
import com.xuexiang.xui.widget.activity.BaseSplashActivity;
import com.xuexiang.xutil.app.ActivityUtils;

import me.jessyan.autosize.internal.CancelAdapt;

/**
 * 启动页【无需适配屏幕大小】
 *
 * @author xuexiang
 * @since 2019-06-30 17:32
 */
public class SplashActivity extends BaseSplashActivity implements CancelAdapt {

    @Override
    protected long getSplashDurationMillis() {
        return 1000;
    }

    /**
     * activity启动后的初始化
     */
    @Override
    protected void onCreateActivity() {
        initSplashView(R.drawable.xui_config_bg_splash);
        startSplash(false);
    }


    /**
     * 启动页结束后的动作
     */
    @Override
    protected void onSplashFinished() {
        if (SettingUtils.isAgreePrivacy()) {
            loginOrGoMainPage();
        } else {
            Utils.showPrivacyDialog(this, (dialog, which) -> {
                dialog.dismiss();
                SettingUtils.setIsAgreePrivacy(true);
                loginOrGoMainPage();
            });
        }
    }

    private void loginOrGoMainPage() {
        if (TokenUtils.hasToken()) {
            String apiUrl = Utils.getWebServerApiUrl(SplashActivity.this,
                    getString(R.string.api_user_me));
            SimpleCallBack<UserDTO> simpleCallBack = new SimpleCallBack<UserDTO>() {
                @Override
                public void onSuccess(UserDTO response) throws Throwable {
                    //开启socket
                    NettyClient nettyClient = NettyClient.newInstance(
                            getString(R.string.server_address),
                            Integer.parseInt(getString(R.string.server_netty_port))
                    );
                    nettyClient.connect();
                    //写入缓存
                    MMKVUtils.put(Constant.MMKV_KEY_USER_DTO, JSON.toJSONString(response));
                    ActivityUtils.startActivity(MainActivity.class);
                    nettyClient.sendMsg(new Packet(
                            Command.USER_ID_REQ, response.getId()
                    ));
                }

                @Override
                public void onError(ApiException e) {
                    ActivityUtils.startActivity(LoginActivity.class);
                    XToastUtils.toast(e.getDisplayMessage());
                }
            };
            Utils.doGet(apiUrl, simpleCallBack);
        } else {
            ActivityUtils.startActivity(LoginActivity.class);
        }
        finish();
    }

    /**
     * 菜单、返回键响应
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return KeyboardUtils.onDisableBackKeyDown(keyCode) && super.onKeyDown(keyCode, event);
    }
}
